package pixelradar.config;

import net.minecraftforge.common.config.Configuration;
import pixelradar.Mw;
import pixelradar.util.Utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WorldConfig {
    private static volatile WorldConfig instance = null;
    public Configuration worldConfiguration;
    public List<Integer> dimensionList = new ArrayList<>();

    private WorldConfig() {
        File worldConfigFile = new File(Mw.getInstance().worldDir, "pixelradar.cfg");
        this.worldConfiguration = new Configuration(worldConfigFile);
        this.initDimensionList();
    }

    public static WorldConfig getInstance() {
        if (instance == null) {
            synchronized (WorldConfig.class) {
                if (instance == null) {
                    instance = new WorldConfig();
                }
            }
        }

        return instance;
    }

    public void saveWorldConfig() {
        this.worldConfiguration.save();
    }

    public void initDimensionList() {
        this.dimensionList.clear();
        this.worldConfiguration.get("world", "dimensionList", Utils.integerListToIntArray(this.dimensionList));
        this.addDimension(0);
        this.cleanDimensionList();
    }

    public void addDimension(int dimension) {
        int i = this.dimensionList.indexOf(dimension);
        if (i < 0) {
            this.dimensionList.add(dimension);
        }

    }

    public void cleanDimensionList() {
        List<Integer> dimensionListCopy = new ArrayList<>(this.dimensionList);
        this.dimensionList.clear();

        for (Integer i : dimensionListCopy) {
            int dimension = i;
            this.addDimension(dimension);
        }

    }
}
